package org.micode.libs.smon.utils;

import org.micode.libs.smon.vo.LocalInfo;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Properties;

public class SystemUtils {

    public static LocalInfo readLocalInfo() {
        LocalInfo info = new LocalInfo();
        fillLocal(info);
        return info;
    }

    public static void fillLocal(LocalInfo info) {
        if (null == info) {
            return;
        }

        Map<String, String> map = System.getenv();
        info.setUserName(map.get("USERNAME"));
        info.setComputerName(map.get("COMPUTERNAME"));
        info.setUserDomain(map.get("USERDOMAIN"));

        try {
            InetAddress addr = InetAddress.getLocalHost();
            info.setIp(addr.getHostAddress());
            info.setHostname(addr.getHostName());
        } catch (UnknownHostException e) {
            info.setIp("UNKNOWN");
            info.setHostname("UNKNOWN");
        }

        Properties props = System.getProperties();

        info.setOsName(props.getProperty("os.name"));
        info.setOsArch(props.getProperty("os.arch"));
        info.setOsVersion(props.getProperty("os.version"));
        info.setFileSeparator(props.getProperty("file.separator"));
        info.setPathSeparator(props.getProperty("path.separator"));
        info.setLineSeparator(props.getProperty("line.separator"));
        info.setUserAccount(props.getProperty("user.name"));
        info.setUserHome(props.getProperty("user.home"));
        info.setUserDir(props.getProperty("user.dir"));
    }

}
